package com.hspedu.homework.homework01;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/3
 * @time 17:33
 */
public class Homework01Client {
    public static void main(String[] args) throws IOException {

        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        boolean flag = true;
        Scanner scanner = null;

            System.out.println("请输入你的问题：");
            scanner = new Scanner(System.in);
            String data = scanner.nextLine();

            OutputStream outputStream = socket.getOutputStream();
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            byte[] bytes = data.getBytes();
            bufferedOutputStream.write(bytes,0,bytes.length);
            bufferedOutputStream.flush();
            socket.shutdownOutput();

            InputStream inputStream = socket.getInputStream();
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            byte[] buf = new byte[1024];
            int readLen = 0;
            while ((readLen = bufferedInputStream.read(buf)) != -1){
                String s = new String(buf, 0, readLen);
                System.out.println(s);
            }

        socket.close();

    }
}
